package com.example.ovulationperiodtracker.Allactivity;

import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.afollestad.materialdialogs.MaterialDialog;
import com.example.ovulationperiodtracker.R;
import com.example.ovulationperiodtracker.databinding.ActivityHuaiyunBinding;
import com.example.ovulationperiodtracker.model.Period;
import com.example.ovulationperiodtracker.utils.JCGSQLiteHelper;
import com.example.ovulationperiodtracker.utils.MyLog;
import com.example.ovulationperiodtracker.view.NoteUtils;
import com.necer.base.BaseActivity;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class HuaiYunActivity extends BaseActivity<ActivityHuaiyunBinding> {


    private DatePickerDialog.OnDateSetListener myDateListener = new DatePickerDialog.OnDateSetListener() {
        public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
            LocalDate localDate = LocalDate.of(arg1, arg2 + 1, arg3);
            if (localDate.isBefore(LocalDate.now())) {
                Toast.makeText(context, "不能在今天之前的日期设置预产期", Toast.LENGTH_SHORT).show();
                return;
            }

            NoteUtils.saveYuChanQi(localDate);
            init(localDate);
        }
    };

    @Override
    protected ActivityHuaiyunBinding createBinding(LayoutInflater inflater) {
        return ActivityHuaiyunBinding.inflate(inflater);
    }

    @Override
    protected void onCreated(Bundle savedInstanceState) {

        LocalDate yuChanQi = NoteUtils.getYuChanQi();
        binding.PinSwitchPregnant.setChecked(yuChanQi != null);
        init(yuChanQi);


        binding.llEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LocalDate yuChanQi = NoteUtils.getYuChanQi();
                new DatePickerDialog(context, R.style.MyDatePickerDialogTheme, myDateListener, yuChanQi.getYear(), yuChanQi.getMonthValue() - 1, yuChanQi.getDayOfMonth()).show();
            }
        });

        binding.PinSwitchPregnant.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    LocalDate lastJingStart = NoteUtils.getLastJingStart();
                    if (lastJingStart == null) {
                        Toast.makeText(context, "请设置上次月经开始日期，否则无法计算预产期", Toast.LENGTH_SHORT).show();
                        binding.PinSwitchPregnant.setChecked(false);
                        return;
                    }
                    LocalDate localDate = lastJingStart.plusDays(280);
                    NoteUtils.saveYuChanQi(localDate);

                } else {
                    NoteUtils.saveYuChanQi(null);
                }
                LocalDate yuChanQi = NoteUtils.getYuChanQi();
                init(yuChanQi);

            }
        });
    }


    private void init(LocalDate yuChanQi) {
        if (yuChanQi == null) {
            binding.llEdit.setVisibility(View.GONE);
            binding.llTextDesPregnant.setVisibility(View.GONE);

        } else {
            binding.llEdit.setVisibility(View.VISIBLE);
            binding.llTextDesPregnant.setVisibility(View.VISIBLE);
            binding.txtDeliveryDate.setText(yuChanQi.toString());
        }
    }


}
